using System.Collections.Generic;

namespace TmxUtil
{
    class ZipNames: Dictionary<string, string>
    {
        public void TryAdd(string fullName)
        {
            this[fullName] = fullName;
        }

        public Dictionary<string, string> Revert()
        {
            Dictionary<string, string> ret = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> pair in this)
            {
                ret[pair.Value] = pair.Key;
            }

            return ret;
        }

        public void Normilize()
        {
            if (Count <= 1)
                return;
            
            char[] separators = new char[] { ':', '/', '\\' };
            while(true)
            {
                string firstKey = GetFirstKey();
                string[] arr0 = firstKey.Split(separators);
                bool isOK = true;
                foreach(string key in Keys)
                {
                    if (key != firstKey)
                    {
                        string[] arr = key.Split(separators);
                        if (arr[0] != arr0[0])
                        {
                            isOK = false;
                            break;
                        }
                    }
                }

                if (!isOK)
                    break;

                int trimLength = arr0[0].Length + 1;
                Dictionary<string, string> copy = new Dictionary<string, string>();
                foreach(string k in Keys)
                {
                    string newKey = k.Substring(trimLength);
                    copy[newKey] = this[k];
                }

                this.Clear();
                foreach(string k in copy.Keys)
                {
                    this[k] = copy[k];
                }
            }
        }

        string GetFirstKey()
        {
            KeyCollection.Enumerator e = this.Keys.GetEnumerator();
            e.MoveNext();
            return e.Current;
        }





    }
}